package cn.chenchao.eshop.user.service.impl;

import cn.chenchao.eshop.user.dao.UserDAO;
import cn.chenchao.eshop.user.dao.impl.UserDAOImpl;
import cn.chenchao.eshop.user.entity.User;
import cn.chenchao.eshop.user.service.UserService;

import java.util.List;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/22 18:14
 * @Author : Chen Chao
 * @File : eshop.cn.chenchao.eshop.user.service.imp.UserServiceImp.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 */
public class UserServiceImpl implements UserService {
    /**
     * 处理用户登录
     * @param user 登录的用户
     * @return 查找到的用户
     * @throws Exception 抛出异常
     */
    @Override
    public User userLogin(User user) throws Exception {
        /*1.调用UserDao获取用户对象列表*/
        UserDAO userDAO = new UserDAOImpl();
        List<User> users = userDAO.getEntityList();

        /*2.循环匹配用户名和密码，如果没有就返回null*/
        for (User userOne : users) {
            if(userOne.getName().equals(user.getName())&&
                    userOne.getPassword().equals(user.getPassword())){
                return userOne;
            }
        }
        return null;
    }

    @Override
    public void userRegister(User user) throws Exception {
        /*1.调用UserDao获取用户对象列表*/
        UserDAO userDAO = new UserDAOImpl();
        /*2. 插入用户数据*/
        userDAO.insert2List(user);
    }
}
